<?php

/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 11/26/2015
 * Time: 1:59 PM
 * Des: 阿里API
 */
class App_ali_appModel extends AppModel
{
    private $app = '720baba_sms';

    /**
     * 获取字段信息
     * @return bool
     */
    public function getField($field)
    {

        $res_cache = $this->setAppInfo($field);
        if ($res_cache === false) {
            return false;
        } else {
            return $res_cache;
        }
    }

    /**
     * 获取key
     */
    public function getKey()
    {
        return $this->getField('key');
    }

    /**
     * 获取密钥
     */
    public function getSecret()
    {
        return $this->getField('secret');
    }

    public function setApp($app)
    {
        $this->app = $app;
    }

    /**
     * 写缓存
     * @param $field
     * @return bool
     */
    public function setAppInfo($field)
    {
        $res = $this->field("key,secret")->find(['app' => $this->app]);
        if ($res === false) {
            return false;
        } else {
            return $res[$field];
        }
    }

    public function getAliAppSiteNameById($id)
    {
        return $this->field('app')->find(['id' => $id])['app'];
    }
    public function getAppSiteName($is_tbk_account=1,$status=1){
        $site_name=$this->field('app')->order('id asc')->find(['is_tbk_account'=>$is_tbk_account,'status'=>$status])['app'];
        if(!empty($site_name)){
             return $site_name; 
        }else{
            return ALI_APP_KEY;
        }
    }
    public function getTbIpBySiteName($site_name){
        $id=$this->field('id')->find(['app'=>$site_name])['id'];
        $ip=M('app.tb_ip')->getIPByAliAppId($id);
        return $ip;
    }
    public function getAllSiteName($is_tbk_account=1,$status=1){
        return $this->field('app,id,site_id')->order('id asc')->select(['is_tbk_account'=>$is_tbk_account,'status'=>$status]);
    }
}
